/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package t5.model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Gabriel Lunardi
 */
public class DisciplinaDAO {
    
public Connection abrir() {
        Connection c = null;
        try {
            Class.forName(BD.JDBC_DRIVER);
            c = DriverManager.getConnection(BD.URL_CONEXAO, BD.USUARIO, BD.SENHA);
        } catch (ClassNotFoundException ecnf) {
            System.out.println("erro ao carregar driver JDBC " + BD.JDBC_DRIVER + " na memória!");
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return c;
    }
    
    
    public List<Disciplina> buscarTodos(){
        Connection conexao = abrir();
        List<Disciplina> disciplinas = new ArrayList<Disciplina>();
        try {
            Statement s = conexao.createStatement();
            ResultSet rs = s.executeQuery("SELECT * FROM disciplina");
            while (rs.next()){
                Disciplina temp = new Disciplina(rs.getString("codDiscip"),rs.getString("nome"),rs.getInt("cargaHoraria"),rs.getInt("sem_acon"));
                disciplinas.add(temp);
            }
            conexao.close();
        } catch (SQLException e){
            e.printStackTrace();
        }
        return disciplinas;
    }
    
}
